package com.example.instantmessage.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.instantmessage.R;
import com.example.instantmessage.adapter.MessageAdapter;
import com.example.instantmessage.adapter.MessageInfoAdapter;
import com.google.android.material.tabs.TabLayout;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMConversationListener;
import com.tencent.imsdk.v2.V2TIMConversationResult;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMSimpleMsgListener;
import com.tencent.imsdk.v2.V2TIMUserInfo;
import com.tencent.imsdk.v2.V2TIMValueCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MessageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MessageFragment extends Fragment {

//    private TabLayout tabLayout;
//    private ViewPager viewPager;
//    private View view;
    private Button testBtn;

    private RecyclerView recyclerView;
    private View view;
    private MessageAdapter messageAdapter;
    private List<V2TIMConversation> conversations = new ArrayList<>();
    private static final String TAG ="MessageInfoFragment" ;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MessageFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MessageFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MessageFragment newInstance(String param1, String param2) {
        MessageFragment fragment = new MessageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_message, container, false);
        initView();
        return view;
    }

    private void initView() {
//        tabLayout = view.findViewById(R.id.sort_message_title);
//        viewPager = view.findViewById(R.id.message_viewpager);

//        MessageInfoFragment friendsFragment = new MessageInfoFragment();
//        MessageInfoFragment groupFragment = new MessageInfoFragment();
//        List<Fragment> fragments = new ArrayList<>();
//        fragments.add(friendsFragment);
//        fragments.add(groupFragment);
//
//        String friendsName = "好友";
//        String groupName = "群聊";
//        List<String> name = new ArrayList<>();
//        name.add(friendsName);
//        name.add(groupName);
//
//        MessageInfoAdapter messageInfoAdapter = new MessageInfoAdapter(getChildFragmentManager(),0,fragments,name);

//        viewPager.setAdapter(messageInfoAdapter);
//        tabLayout.setupWithViewPager(viewPager);//???

//        System.out.println("111111 initView");
//        testBtn = view.findViewById(R.id.test_btn);
//        testBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                String userId = "xiaozhufeifei";
//
//                V2TIMManager.getInstance().sendC2CTextMessage("hell王五", userId, new V2TIMValueCallback<V2TIMMessage>() {
//                    @Override
//                    public void onSuccess(V2TIMMessage v2TIMMessage) {
//                        Log.i(TAG, "onSuccess: 111111");
//                    }
//
//                    @Override
//                    public void onError(int code, String desc) {
//                        Log.i(TAG, "onError: 2222222:" + code + desc);
//
//                    }
//                });
//
//
//                V2TIMManager.getInstance().addSimpleMsgListener(new V2TIMSimpleMsgListener() {
//                    @Override
//                    public void onRecvC2CTextMessage(String msgID, V2TIMUserInfo sender, String text) {
//                        super.onRecvC2CTextMessage(msgID, sender, text);
//                    }
//                });
//
//
//
//
//            }
//        });






        recyclerView = view.findViewById(R.id.message_info_recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        messageAdapter = new MessageAdapter(getContext(),conversations);
        recyclerView.setAdapter(messageAdapter);

        V2TIMManager.getConversationManager().setConversationListener(new V2TIMConversationListener() {


            @Override
            public void onNewConversation(List<V2TIMConversation> conversationList) {
                super.onNewConversation(conversationList);
                Toast.makeText(getContext(),"新会话",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onConversationChanged(List<V2TIMConversation> conversationList) {
                super.onConversationChanged(conversationList);


            }

            @Override
            public void onTotalUnreadMessageCountChanged(long totalUnreadCount) {
                super.onTotalUnreadMessageCountChanged(totalUnreadCount);
            }
        });

        V2TIMManager.getConversationManager().getConversationList(0, 50,
                new V2TIMValueCallback<V2TIMConversationResult>() {


                    @Override
                    public void onError(int code, String desc) {
                        System.out.println(conversations.size()+"111111  error"+Thread.currentThread());
// 拉取会话列表失败
                    }

                    @Override
                    public void onSuccess(V2TIMConversationResult v2TIMConversationResult) {
                        List<V2TIMConversation> conversationList =  v2TIMConversationResult.getConversationList();
//                        conversations.clear();
//                        conversations.addAll(conversationList);
                        conversations = conversationList;
                        messageAdapter.notifyDataSetChanged();
                        System.out.println(conversations.size()+"111111"+Thread.currentThread().getId());

                    }
                });

    }
}